package server.business.utils;


import com.mysql.cj.jdbc.Driver;
import server.main.utils.mainutils.UrlUtils;
import server.main.utils.mainutils.ymlUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Map;

public class Dbutils {



    public static Object chargedatabase(String sql) throws Exception {

        boolean flag = true;
        String ymlpath = UrlUtils.GetResourcesUrl() + "application.yml";
        Map datasource = ymlUtils.getDatasource(ymlpath);
        DriverManager.registerDriver(new Driver());
        System.out.println(datasource.get("username"));
        String username = (String) datasource.get("username");
        String password = datasource.get("password").toString();
        String url = (String) datasource.get("url");
//        String url2 = "jdbc:mysql://localhost:3306/bloodtransfusionmanagement"
        Connection conn = null;
        Statement stat = null;
        //创建表test
        try{
            System.out.println(username+" " + password + " " + url);
            conn = DriverManager.getConnection(url, username, password);
            stat = conn.createStatement();

            stat.executeUpdate(sql);
        }catch (Exception e){
            flag = false;
            e.printStackTrace();
        }finally {
            stat.close();
            conn.close();

        }
        return flag;
    }
}
